﻿// ***********************************************************************
// Provided for Informational Purposes Only
//
// Apache 2.0 License
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.Models
// File      : YammerCredentials.cs
//
// ***********************************************************************
using System;


namespace ContractMeow.Models
{
	/// <summary>
	/// Class YammerCredentials
	/// </summary>
	public sealed class YammerCredentials
	{
		#region Ctors
		/// <summary>
		/// Initializes a new instance of the <see cref="ContractMeow.Api.Dtos.Principal" /> class.
		/// </summary>
		public YammerCredentials() { }

		/// <summary>
		/// Initializes a new instance of the <see cref="ContractMeow.Api.Dtos.Principal" /> class.
		/// </summary>
		/// <param name="principal">The principal.</param>
		internal YammerCredentials(ContractMeow.Api.Dtos.Principal principal)
		{
			UserId = principal.UserId;
			NetworkId = principal.NetworkId;
			AuthToken = new Token
			{
				AuthenticatedAt = principal.AuthorizedAt,
				ExpiresAt = principal.ExpiresAt,
				//EncryptedKey = principal.AccessToken.Protect()
				Key = principal.AccessToken
			};
		}
		#endregion

		/// <summary>
		/// Gets or sets the (cacheable) id.
		/// </summary>
		/// <value>The id.</value>
		public long Id
		{
			get { return UserId; }
		}

		/// <summary>
		/// Gets or sets the user id.
		/// </summary>
		/// <value>The user id.</value>
		public Int64 UserId { get; set; }

		/// <summary>
		/// Gets or sets the network id.
		/// </summary>
		/// <value>The network id.</value>
		public Int64 NetworkId { get; set; }

		/// <summary>
		/// Gets or sets the auth token.
		/// </summary>
		/// <value>The auth token.</value>
		public Token AuthToken { get; set; }

		/// <summary>
		/// Gets a value indicating whether this instance is valid.
		/// </summary>
		/// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
		public bool IsValid
		{
			get { return true; }
		}

		/// <summary>
		/// Performs an implicit conversion from <see cref="YammerCredentials" /> to <see cref="ContractMeow.Api.Dtos.Principal" />.
		/// </summary>
		/// <param name="credentials">The credentials.</param>
		/// <returns>The result of the conversion.</returns>
		public static implicit operator ContractMeow.Api.Dtos.Principal(YammerCredentials credentials)
		{
			return new ContractMeow.Api.Dtos.Principal()
			{
				UserId = credentials.UserId,
				NetworkId = credentials.NetworkId,
				AccessToken = credentials.AuthToken.Key,
				AuthorizedAt = credentials.AuthToken.AuthenticatedAt,
				ExpiresAt = credentials.AuthToken.ExpiresAt
			};
		}

		/// <summary>
		/// Performs an explicit conversion from <see cref="ContractMeow.Api.Dtos.Principal" /> to <see cref="YammerCredentials" />.
		/// </summary>
		/// <param name="principal">The principal.</param>
		/// <returns>The result of the conversion.</returns>
		public static explicit operator YammerCredentials(ContractMeow.Api.Dtos.Principal principal)
		{
			return new YammerCredentials(principal);
		}
	}
}
